from random import random

import requests
from lxml import etree


def get_redirect_url(url):
    # 重定向前的链接
    # url = r'https://www.douban.com/link2/?url=http%3A%2F%2Fwww.iqiyi.com%2Fv_19rrk3u264.html%3Fvfm%3Dm_331_dbdy%26fv%3D4904d94982104144a1548dd9040df241&subtype=9&type=online-video'
    # 请求头，这里我设置了浏览器代理
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'}

    # 请求网页
    response = requests.get(url, headers=headers)
    # e = etree.HTML(response.text)
    # print(e.xpath('//div[@id="mainpic"]/a/img/@src'))
    # print(response.status_code)  # 打印响应的状态码
    # print(response.text)  # 打印重定向后的网址
    # print(response.headers)
    # print(response.url)  # 打印重定向后的网址

    # 返回重定向后的网址
    return response.url


if __name__ == '__main__':
    redirect_url = get_redirect_url()